package com.yonyougov.devops.cruxcisupport.k8s.rest.controller;

import com.yonyougov.devops.cruxcisupport.k8s.pod.PodStatusInfo;
import com.yonyougov.devops.cruxcisupport.k8s.rest.*;
import com.yonyougov.devops.cruxcisupport.k8s.rest.service.K8sDeploymentService;
import com.yonyougov.devops.cruxcisupport.k8s.rest.vo.PodEventVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.api.enums.BaseErrorCode;
import io.swagger.annotations.Api;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "调用k8sAPI创建相关资源")
@Slf4j
@RestController
@RequestMapping("/api/v2/kubernetes")
public class K8sDeploymentController {
    private final K8sDeploymentService deploymentService;

    public K8sDeploymentController(K8sDeploymentService deploymentService) {
        this.deploymentService = deploymentService;
    }

    @PostMapping(value = "/pods")
    @SneakyThrows
    public BaseResponseDto createPod(@RequestBody @Valid DeploymentRestRequest request){
        deploymentService.createDeployment(request);
        return BaseResponseDto.getSuccessInstance();
    }

    @PostMapping(value = "/batchpods")
    @SneakyThrows
    public BaseResponseDto createPods(@RequestBody @Valid BatchDeploymentRestRequest request){
        deploymentService.batchCreateDeployment(request);
        return BaseResponseDto.getSuccessInstance();
    }

    @PostMapping(value = "/pod/names")
    @SneakyThrows
    public BaseResponseDto getPodNames(@RequestBody @Valid PodNameRestRequest request){
        List<String> podNameList = deploymentService.getPodNames(request);
        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("data",podNameList);
        return baseResponseDto;
    }

    @PostMapping(value = "/pod/log")
    @SneakyThrows
    public BaseResponseDto getPodLog(@RequestBody @Valid PodLogRestRequest request){
        String podLog = deploymentService.getPodLog(request);
        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("data",podLog);
        return baseResponseDto;
    }

    @PostMapping(value = "/pod/details")
    @SneakyThrows
    public BaseResponseDto getPodDetails(@RequestBody @Valid  PodDetailsRestRequest request){
        List<PodStatusInfo> podStatusInfoList = deploymentService.getPodDetailsByNameSpace(request);
        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("data",podStatusInfoList);
        return baseResponseDto;
    }

    @PostMapping(value = "/pod/batchdel")
    @SneakyThrows
    public BaseResponseDto deleteBatchPod(@RequestBody @Valid  PodBatchDelRestRequest request){
        boolean result = deploymentService.deleteBatchPod(request);
        if(result){
            return BaseResponseDto.getSuccessInstance();
        }else{
            return BaseResponseDto.getFailInstance(BaseErrorCode.SYS_ERROR);
        }
    }

    @PostMapping(value = "/pod/events")
    @SneakyThrows
    public BaseResponseDto getEventsByPodName(@RequestBody @Valid  PodEventRestRequest request){
        List<PodEventVo> data = deploymentService.getEventsByPodName(request);
        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("data",data);
        return baseResponseDto;
    }
}
